package com.zztl.infiniteBand.ui.activity

import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.View
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseActivity
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.model.ContactInfoBean
import com.zztl.infiniteBand.model.FirmwareVersionBean
import com.zztl.infiniteBand.model.VersionResponseBean
import com.zztl.infiniteBand.presenter.ContactPresenter
import com.zztl.infiniteBand.storage.BillPreference
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.storage.PreferenceKeys
import com.zztl.infiniteBand.ui.view.dialog.ExitDialog
import com.zztl.infiniteBand.ui.view.mvpview.ContactInfoView
import com.zztl.infiniteBand.util.PermissionUtils
import com.zztl.infiniteBand.util.UiUtils
import kotlinx.android.synthetic.main.activity_contact_us.*

class ContactUsActivity : BaseMvpActivity<ContactInfoView,ContactPresenter>(),ContactInfoView {

    override fun createPresenter(): ContactPresenter {
        return ContactPresenter()
    }

    /*  @BindView(R.id.item_phone)
      lateinit var mItemPhone: CommonItemView
      @BindView(R.id.item_qq)
      lateinit var mItemQq: CommonItemView*/


    private var mPhoneDialog: ExitDialog? = null


    private val mPermissionGrant = object : PermissionUtils.PermissionGrant {
        override fun onPermissionGranted(requestCode: Int) {
            when (requestCode) {
                PermissionUtils.CODE_CALL_PHONE -> try {
                    showPhoneDialog()
                } catch (e: Exception) {

                }

                else -> {
                }
            }
        }
    }

    override fun setLayoutId(): Int = R.layout.activity_contact_us


    override fun initBase() {
        setTitle(getString(R.string.联系我们))
        item_qq.rightTv.setTextIsSelectable(true)
        item_twitter.rightTv.setTextIsSelectable(true)
        item_tele.rightTv.setTextIsSelectable(true)
       /* BillPreference.getObjFromSp<ContactInfoBean.DataBean>(PreferenceKeys.CONTACTINFO.name)?.let { bean ->
            item_phone.rightTextStr = Constans.CONTACT_PHONE_NUM
            item_qq.rightTextStr = bean.qq1
            item_twitter.rightTextStr = bean.qq2
            item_tele.rightTextStr = bean.telegram
        }*/


        item_phone.setOnClickListener { callPhone() }
        item_phone.rightImg.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                callPhone()
            }

        })

        presenter?.getContactInfo(this)
    }
    override fun update(bean: VersionResponseBean) {

    }

    override fun getVersion(bean: FirmwareVersionBean) {

    }

    override fun getSuccess(bean: ContactInfoBean) {
        BillPreference.getObjFromSp<ContactInfoBean.DataBean>(PreferenceKeys.CONTACTINFO.name)?.let{
            if(bean.data.toString()!=it.toString()){
                BillPreference.saveObj2SP(PreferenceKeys.CONTACTINFO.name, bean.data)
                bean.data?.phone?.let {phone->
                    Constans.CONTACT_PHONE_NUM = phone }
            }

        }

        bean.data?.run {
            item_phone.rightTextStr =this.phone
            item_qq.rightTextStr =this.qq1
            item_twitter.rightTextStr  =this.qq2
            item_tele.rightTextStr  =this.telegram
        }
    }

    private fun callPhone() {
        PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant)

    }


    private fun showPhoneDialog() {
        if (!TextUtils.isEmpty(Constans.PHONENUMBER)) {
            if (null == mPhoneDialog) {
                mPhoneDialog = ExitDialog.Builder(this).setMessage(UiUtils.getString(R.string.是否拨打电话) + Constans.CONTACT_PHONE_NUM)
                        .setClickListener(object : ExitDialog.Builder.ClickListener {
                            override fun cancel() {

                            }

                            override fun comfirm() {
                                callPhone(Constans.CONTACT_PHONE_NUM)
                            }
                        }).create()
            }

            mPhoneDialog!!.show()
        }
    }


    fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_CALL)
        val data = Uri.parse("tel:$phoneNum")
        intent.data = data
        showActivity(intent)
    }

}
